package com.hw.one.collect.ionsoup.collect;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;

import com.hw.one.collect.config.ConditionConfig;
import com.hw.one.collect.entity.CommonData;
import com.hw.one.collect.entity.DataCollector;
import com.hw.one.collect.entity.DataTypeEnum;
import com.hw.one.collect.ionsoup.entity.AirCycle;
import com.hw.one.collect.ionsoup.entity.Results;
import com.hw.one.collect.ionsoup.util.AirConcUtil;
import com.hw.one.transpot.UploadDataUtil;

/**
 * @Class ModbusDefaultDataCollector
 * @Author guoshenzhen
 * @Date 2018/3/14 13:16
 */
public class AirDefaultDataCollector implements DataCollector<CommonData> {
	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * 采集数据
	 * 
	 * @param key 如果为正常采集时，key为空； 如果是容错采集时， key非空
	 * @return
	 * @throws Exception 
	 */
	@SuppressWarnings("finally")
	@Override
	public CommonData collect(String dateTime) {
		CommonData data = new CommonData(DataTypeEnum.IONSOUP_AIR);
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
			SimpleDateFormat sdff = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			// 创建备忘录
			String filePath = ConditionConfig.get("airFilePath", ConditionConfig.ionSoup);
			Results<AirCycle> parse = new AirConcUtil().parse(filePath);
			
			if (parse != null && parse.getLiquidCycleList() != null) {
				List<AirCycle> liquidCycleList = parse.getLiquidCycleList();
				String end = "";
				for (AirCycle airCycle : liquidCycleList) {
					end = sdf.format(sdff.parse(airCycle.getEnd()));
					if (end.equals(dateTime)) {
						data.setList(new ArrayList<>(Arrays.asList(airCycle)));
					}
				}
			}
		} catch (Exception e) {
			log.debug(e);
			return null;
		} finally {
			if (data != null && data.getList() != null && data.getList().size() > 0) {
				return data;
			} else {
				return null;
			}
		}
	}
	
	public static void main(String[] args) throws Exception {
		AirDefaultDataCollector c = new AirDefaultDataCollector();
		c.collect("2018121715");
	}
}
